import http from "@/utils/http";
import { HttpResponse, checkHttpError } from "../common";
import { FORM_DATA_AXIOS_CONFIG } from "../constants";

/**
 * 获得所有的用户组
 * @param type 用户组类别
 */
export async function getUserGroupData(type: number): Promise<UserGroupItem[]> {
  const resp: HttpResponse = await http.get(
    `/Emgroup/listallemgroup/?emgtype=${type}`
  );
  checkHttpError(resp);
  return resp.detail;
}

/**
 * 获得未分配的用户，通过用户组ID
 */
export async function getNoUsersDataByGroup(
  group: string
): Promise<GroupUserItem[]> {
  const resp: HttpResponse = await http.get(`/Emgroup/${group}/showallnouser/`);
  checkHttpError(resp);
  return resp.detail;
}

/**
 * 获得已分配的用户，通过用户组ID
 */
export async function getUsersDataByGroup(
  group: string
): Promise<GroupUserItem[]> {
  const resp: HttpResponse = await http.get(`/Emgroup/${group}/showalluser/`);
  checkHttpError(resp);
  return resp.detail;
}

/**
 * 创建用户组
 * @param param0
 */
export async function createUserGroup({
  emgname,
  emgtype,
  detail,
  order,
  remark
}) {
  const form = new FormData();
  form.append("emgname", emgname);
  form.append("emgtype", emgtype);
  form.append("detail", detail);
  form.append("order", order);
  form.append("remark", remark);

  const resp: HttpResponse = await http.post(
    "Emgroup/createemgroup/",
    form,
    FORM_DATA_AXIOS_CONFIG
  );
  checkHttpError(resp);
  return resp.detail;
}

export async function updateUserGroup({
  emgid,
  emgname,
  emgtype,
  detail,
  order,
  remark
}) {
  const form = new FormData();
  form.append("emgname", emgname);
  form.append("emgtype", emgtype);
  form.append("detail", detail);
  form.append("order", order);
  form.append("remark", remark);

  const resp: HttpResponse = await http.post(
    `/Emgroup/${emgid}/updateemgroup/`,
    form,
    FORM_DATA_AXIOS_CONFIG
  );

  checkHttpError(resp);
  return resp.detail as string;
}

/**
 * 删除用户组根据ID
 * @param id
 */
export async function deleteUserGroup(id: string) {
  const resp: HttpResponse = await http.get(`/Emgroup/${id}/delemgroup/`);
  return checkHttpError(resp) ? (resp.detail as string) : false;
}

export async function addUserToGroup(uid: string, groupId: string) {
  const form = new FormData();
  form.append("emid", uid);
  form.append("emgid", groupId);

  const resp: HttpResponse = await http.post(
    "/Emgroup/manningemgroup/",
    form,
    FORM_DATA_AXIOS_CONFIG
  );

  checkHttpError(resp);
  return resp.detail as string;
}

export async function removeUserFromGroup(uid: string, groupId: string) {
  const form = new FormData();
  form.append("emid", uid);
  form.append("emgid", groupId);

  const resp: HttpResponse = await http.post(
    "/Emgroup/unmanningemgroup/",
    form,
    FORM_DATA_AXIOS_CONFIG
  );

  checkHttpError(resp);
  return resp.detail as string;
}

export type UserGroupItem = {
  detail: string;
  dt_create: string;
  emgid: string;
  emgname: string;
  emgtype: number;
  emid_create: string;
  isdel: number;
  order: number;
  remark: string;
  shortcpname: string;
};

/**
 * 用户组中的用户列表的条目结构
 */
export type GroupUserItem = {
  QQ: string;
  cnname: string;
  id: string;
  mobile: string;
  username: string;
};
